package com.example.videodisplay;

import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.WindowInsetsController;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;

import androidx.appcompat.app.AppCompatActivity;

/**
 * @author xiaotie
 */
public class PlayActivity extends AppCompatActivity {

    private String s;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //开启硬件加速
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
                WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
        setContentView(R.layout.activity_play);
        s = "https://jx.quanmingjiexi.com/?url=https://www.iqiyi.com/v_19rwzq6lpc.html";
//        s = getIntent().getStringExtra(PlayConst.SIGN);
        initWidget();
    }

    /**
     * 获得或失去焦点时调用
     * @param hasFocus 是否获取焦点
     */
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if(hasFocus){
            hideSystemUI();
        }
    }

    /**
     *隐藏系统条
     */
    private void hideSystemUI() {
        View decorView = getWindow().getDecorView();
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.R){
            WindowInsetsController controller = decorView.getWindowInsetsController();
            //当系统条在此模式下隐藏时，它们可以通过系统手势临时显示
            controller.hide(WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
        }else {
            decorView.setSystemUiVisibility(
                    //将内容设置为显示在系统栏下，以便系统栏隐藏和显示时内容不会调整大小。
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            //隐藏导航条和状态栏
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                            | View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        }
    }

    /**
     * 加载视频
     */
    @SuppressLint("SetJavaScriptEnabled")
    private void initWidget() {
        WebView webView = findViewById(R.id.webView);
        WebSettings webSettings= webView.getSettings();
        webSettings.setDefaultTextEncodingName("utf-8") ;
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webView.setWebChromeClient(new WebChromeClient());
        webView.loadUrl(s);
    }
}
